#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>//rand()
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;
using std::queue;

queue<int> q;
int n,m,a[111][11],dp[4444],inf = 1e9+9;

inline int DO(int now,int p){
    rep(i,1,n){
        if(a[p][i] == 1) if((1<<(i-1)) & now) now -= (1<<(i-1));
        if(a[p][i] == -1) now |= (1<<(i-1)); 
    }
    return now;
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> n >> m;
    rep(i,1,m) rep(j,1,n) cin >> a[i][j];
    rep(i,0,1<<n) dp[i] = inf;
    q.push((1<<n)-1);
    dp[(1<<n)-1] = 0;
    while(q.size()){
        int now = q.front();
        rep(i,1,m){
            int nnow = DO(now,i);
            if(dp[nnow] > dp[now]+1){dp[nnow] = dp[now]+1;q.push(nnow);}
            if(nnow == 0){cout << dp[now]+1 << "\n";return 0;}
        }
        q.pop();
    }

    cout << "-1\n";
    return 0;
}